<template>
  <div>
    <el-button
      type="primary"
      class="anniu"
      @click="addArea"
    >添加区域</el-button>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list">
        <el-table-column
          prop="index"
          label="序号"
          type="index"
          :index="indexMethod"
        />
        <el-table-column label="区域名称" width="180" prop="areaName" />
        <el-table-column label="车位数(个)" prop="spaceNumber" />
        <el-table-column label="面积(m²)" width="120" prop="areaProportion" />
        <el-table-column label="计费规则" width="120" prop="ruleName" />
        <el-table-column label="备注" prop="remark" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button
              size="mini"
              type="text"
              @click="editfrom(row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="deletelist(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      :title="`${addForm.id ? '编辑区域' : '添加区域'}`"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="rules">
          <el-form-item label="区域名称" prop="areaName">
            <el-input v-model="addForm.areaName" placeholder="请输入区域名称" />
          </el-form-item>
          <el-form-item label="车位数(个)" prop="spaceNumber">
            <el-input v-model="addForm.spaceNumber" placeholder="请输入车位个数" />
          </el-form-item>
          <el-form-item label="面积(m²)" prop="areaProportion">
            <el-input v-model="addForm.areaProportion" placeholder="请输入面积">
              <template slot="append">m²</template>
            </el-input>
          </el-form-item>
          <el-form-item label="关联计费规则" prop="ruleId">
            <el-select v-model="addForm.ruleId" placeholder="请选择" class="fenpei">
              <el-option
                v-for="item in roleList"
                :key="item.ruleId"
                :label="item.ruleName"
                :value="item.ruleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="addForm.remark" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="determine"
        >确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { viewAPI, deleteAPI, determineAPI, dropListcarRuleAPI, EditRegionAPI } from '@/api/area'
export default {
  data() {
    return {
      list: [],
      roleList: [],
      page: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      params: {
        page: 1,
        pageSize: 10
      },
      addForm: {
        id: '',
        ruleId: '',
        areaName: '',
        spaceNumber: '',
        areaProportion: '',
        remark: ''
      },
      rules: {
        areaName: [
          { required: true, message: '请输入区域名称', trigger: 'blur' }
        ],
        spaceNumber: [
          { required: true, message: '请输入车位数(个)', trigger: 'blur' }
        ],
        areaProportion: [
          { required: true, message: '请输入面积(m²)', trigger: 'blur' }
        ],
        ruleId: [
          { required: true, message: '请选择关联计费规则', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.view() // 列表渲染
    this.select() // 下拉列表
  },
  methods: {
    /**
     * 下拉列表**********************************************************
     */
    async select() {
      const res = await dropListcarRuleAPI()
      console.log(res)
      this.roleList = res.data
    },
    /**
     * 添加与编辑*******************************************************************
     */
    determine() {
      this.$refs.addForm.validate(async(val) => {
        if (!val) return
        if (this.addForm.id) {
          await EditRegionAPI({
            id: this.addForm.id,
            ruleId: this.addForm.ruleId,
            areaName: this.addForm.areaName,
            spaceNumber: this.addForm.spaceNumber,
            areaProportion: this.addForm.areaProportion,
            remark: this.addForm.remark
          })
          this.$message.success('编辑成功')
        } else {
          await determineAPI({ ruleId: this.addForm.ruleId,
            areaName: this.addForm.areaName,
            spaceNumber: this.addForm.spaceNumber,
            areaProportion: this.addForm.areaProportion,
            remark: this.addForm.remark })
          this.$message.success('添加成功')
        }
        this.view() // 列表渲染
        this.dialogVisible = false

        this.addForm.remark = ''
      })
    },
    //* *********************************************** */
    /**
     * 回显
     * @param {*} row
     */
    async editfrom(row) {
      this.dialogVisible = true
      this.addForm = row
    },
    addArea() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
      this.addForm = ''
      this.addForm.remark = ''
      this.addForm.id = ''
    },
    /**
     * 删除*********************************************************************
     * @param {*} id
     */
    async  deletelist(id) {
      this.$confirm('确认删除该楼宇吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteAPI(id)
        // this.$message.success('楼宇已删除')
        this.view() // 列表渲染
      })
    },
    /**
     * 列表渲染*****************************************************************
     */
    async view() {
      const res = await viewAPI(this.params)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total // 总条数
    },
    /**
     * 分页*************************************************************************
     * @param {*} index
     */
    indexMethod(index) {
      return index + 1 + (this.params.page - 1) * this.params.pageSize
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.view(val)
      this.params.page = 1
      this.view()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.params.page = val
      this.view()
    }
  }
}
</script>
<style lang="scss" scoped>
.fenpei{
  width: 100%;
}
.anniu {
  font-size: 15px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.department-container {
  padding: 10px;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-label {
    width: 100px;
  }
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
</style>
